package br.com.seimos.flex.component
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.controls.Label;
	import mx.controls.Text;
	import mx.events.FlexEvent;
	import mx.events.ScrollEvent;
	import mx.messaging.messages.RemotingMessage;
	import mx.rpc.events.InvokeEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class SDataGrid extends DataGrid
	{
		public function SDataGrid()
		{
			super();
			showScrollTips=true;
			//addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void
			addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void
			{
				addNavigationTools()
			});
			addEventListener(ScrollEvent.SCROLL, function(event:ScrollEvent):void
			{
				if (dataProvider.length - rowCount <= verticalScrollPosition)
				{
					btnMore.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
					btnMore.enabled=false;
					btnAll.enabled=false;
				}
			});
			
			addEventListener(FlexEvent.VALUE_COMMIT, function(event:FlexEvent):void
			{
				text.text = collection.length + " linha(s) mostradas"
			});
		}
		private var btnMore:Button=new Button();
		private var btnAll:Button=new Button();
		private var pagingArgs:ArrayCollection=new ArrayCollection();
		private var newArgs:ArrayCollection=new ArrayCollection();
		private var text:Text = new Text();

		private function addNavigationTools():void
		{
			btnMore.enabled=false;
			btnMore.label="Mais...";
			btnMore.addEventListener(MouseEvent.CLICK, buttonClick_handler);
			btnMore.addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void
			{
				if (dataProvider.length)
				{
					btnMore.enabled=true;
				}
			});
			btnAll.enabled=false;
			btnAll.label="Todos";
			btnAll.addEventListener(MouseEvent.CLICK, buttonClick_handler);
			btnAll.addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void
			{
				if (dataProvider.length)
				{
					btnAll.enabled=true;
				}
			});
			
			text.text = "0 linha(s) mostradas";
			
			var hBox:HBox=new HBox();
			hBox.width=width;
			hBox.addChild(btnMore);
			hBox.addChild(btnAll);
			hBox.addChild(text);
			parent.addChildAt(hBox, parent.getChildIndex(parent.getChildByName(name)) + 1);
		}

		//--------------------------------------------------------------------------
		//
		//  Private
		//
		//--------------------------------------------------------------------------
		private function buttonClick_handler(event:MouseEvent):void
		{
			pagingArgs.setItemAt(dataProvider.length, 1);
			if (event.currentTarget == btnMore)
			{
				pagingArgs.setItemAt(0, 2);
			}
			else if (event.currentTarget == btnAll)
			{
				pagingArgs.setItemAt(1, 2);
			}
			pagingButtonPerformed=true;
			btnMore.enabled=false;
			btnAll.enabled=false;
			_remoteObject.getOperation(operation).send.apply(null, pagingArgs.toArray());
		}

		private function addNewPage(event:ResultEvent):void
		{
			if (ArrayCollection(event.result).length)
			{
				ArrayCollection(dataProvider).addAll(event.result as ArrayCollection);
				btnMore.enabled=true;
				btnAll.enabled=true;
				text.text = dataProvider.length + " linha(s) mostradas"
			}
			else
			{
				btnMore.enabled=false;
				btnAll.enabled=false;
			}
		}
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var pagingButtonPerformed:Boolean=false;
		private var _remoteObject:RemoteObject;

		/**
		 * RemoteObject responsável pela população do dataGrid corrente;
		 *
		 * @return
		 */
		[Bindable]
		public function get remoteObject():RemoteObject
		{
			return _remoteObject;
		}

		public function set remoteObject(value:RemoteObject):void
		{
			_remoteObject=value;
			_remoteObject.convertParametersHandler=function(args:Array):Array
			{
				if (pagingButtonPerformed)
				{
					pagingArgs.setItemAt("pagedGrid", 0);
					pagingArgs.setItemAt(dataProvider.length, 1);
					for (var i:int = 3; i < args.length; i++)
					{
						pagingArgs.setItemAt(args[i], i);
					}
				}
				else
				{
					var newArgs:ArrayCollection=new ArrayCollection(args);
					newArgs.addItemAt("pagedGrid", 0);
					newArgs.addItemAt(0, 1);
					newArgs.addItemAt(0, 2);
					pagingArgs=new ArrayCollection();
					pagingArgs.addAll(newArgs);
				}
				return pagingArgs.toArray();
			};
			_remoteObject.addEventListener(InvokeEvent.INVOKE, function(event:InvokeEvent):void
			{
				if (RemotingMessage(event.message).operation == operation)
				{ // verifica se o serviço que também faz a paginação foi acionado
					_remoteObject.getOperation(operation).removeEventListener(ResultEvent.RESULT, addNewPage);
					if (pagingButtonPerformed)
					{
						_remoteObject.getOperation(operation).addEventListener(ResultEvent.RESULT, addNewPage);
					}
					btnMore.enabled=true;
					btnAll.enabled=true;
				}
				else
				{
					var a:ArrayCollection=new ArrayCollection(event.message.body as Array);
					a.removeItemAt(2);
					a.removeItemAt(1);
					a.removeItemAt(0);
				}
				pagingButtonPerformed=false;
			});
		}
		private var _operation:String;

		public function get operation():String
		{
			return _operation;
		}

		public function set operation(value:String):void
		{
			_operation=value;
		}
	}
}